package com.xxl.job.admin.util;

import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.dao.XxlJobInfoDao;
import com.xxl.job.admin.service.impl.XxlJobServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;


/**
 * @ProjectName: HIM
 * @Package: com.xxl.job.admin.util
 * @ClassName: InitTask
 * @Author: tangguangyuan
 * @Description:启动系统时添加默认的定时任务
 * @Date: 2021/6/22 15:43
 * @Version: 1.0
 */
@Component
@Slf4j
public class InitTask implements ApplicationRunner {
    @Autowired
    private XxlJobServiceImpl xxlJobInfoService;
    @Autowired
    private XxlJobInfoDao xxlJobInfoDao;

    @Value("${scene.ip}")
    private String sceneIp;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        boolean result;
        log.info("---------开始初始化定时任务----------");

        //删除现有的系统定时任务
        log.info("---------删除原系统定时任务----------");
        xxlJobInfoDao.deleteSystem();

        //初始化场景密钥的定时任务
        log.info("---------初始化定时任务：定时修改场景密钥----------");
        result = initSceneKey();
        log.info("---------初始化定时任务：定时修改场景密钥----------" + (result ? "成功" : "失败"));
        //初始化更新天气的定时任务
        log.info("---------初始化定时任务：每间隔10分钟在缓存中更新天气条件表中包含的城市天气----------");
        result = initCacheWeather();
        log.info("---------初始化定时任务：每间隔10分钟在缓存中更新天气条件表中包含的城市天气----------" + (result ? "成功" : "失败"));

        //初始化场景密钥的定时任务
        log.info("---------初始化定时任务：定时执行天气定时任务----------");
        result = initExecWeatherCron();
        log.info("---------初始化定时任务：定时执行天气定时任务----------" + (result ? "成功" : "失败"));
    }

    public boolean initSceneKey() {
        XxlJobInfo xxlJobInfo = new XxlJobInfo();
        xxlJobInfo.setJobGroup(1);
        xxlJobInfo.setJobDesc("定时修改场景密钥");
        xxlJobInfo.setAuthor("SYSTEM");
        xxlJobInfo.setScheduleType("CRON");
        xxlJobInfo.setScheduleConf("0 */10 * * * ? *");
        xxlJobInfo.setMisfireStrategy("DO_NOTHING");
        xxlJobInfo.setExecutorRouteStrategy("FIRST");
        xxlJobInfo.setExecutorHandler("redisJobHandler");
        xxlJobInfo.setExecutorBlockStrategy("SERIAL_EXECUTION");
        xxlJobInfo.setExecutorTimeout(0);
        xxlJobInfo.setExecutorFailRetryCount(0);
        xxlJobInfo.setGlueType("BEAN");
        xxlJobInfo.setGlueRemark("GLUE代码初始化");
        xxlJobInfoService.add(xxlJobInfo);
        return true;
    }

    public boolean initCacheWeather() throws UnknownHostException {

        XxlJobInfo xxlJobInfo = new XxlJobInfo();
        xxlJobInfo.setJobGroup(1);
        xxlJobInfo.setJobDesc("每间隔10分钟在缓存中更新天气条件表中包含的城市天气");
        xxlJobInfo.setAuthor("SYSTEM");
        xxlJobInfo.setScheduleType("CRON");
        xxlJobInfo.setScheduleConf("0 */10 * * * ? *");
        xxlJobInfo.setMisfireStrategy("DO_NOTHING");
        xxlJobInfo.setExecutorRouteStrategy("FIRST");
        xxlJobInfo.setExecutorHandler("httpJobHandler");
        xxlJobInfo.setExecutorParam("url:http://" + sceneIp + ":21000/scene/timer/updateWeather\n" +
                "method:GET\n" +
                "data:");
        xxlJobInfo.setExecutorBlockStrategy("SERIAL_EXECUTION");
        xxlJobInfo.setExecutorTimeout(0);
        xxlJobInfo.setExecutorFailRetryCount(0);
        xxlJobInfo.setGlueType("BEAN");
        xxlJobInfo.setGlueRemark("GLUE代码初始化");
        xxlJobInfoService.add(xxlJobInfo);
        return true;
    }

    public boolean initExecWeatherCron() throws UnknownHostException {

        XxlJobInfo xxlJobInfo = new XxlJobInfo();
        xxlJobInfo.setJobGroup(1);
        xxlJobInfo.setJobDesc("每间隔10分钟执行天气的定时任务");
        xxlJobInfo.setAuthor("SYSTEM");
        xxlJobInfo.setScheduleType("CRON");
        xxlJobInfo.setScheduleConf("0 */10 * * * ? *");
        xxlJobInfo.setMisfireStrategy("DO_NOTHING");
        xxlJobInfo.setExecutorRouteStrategy("FIRST");
        xxlJobInfo.setExecutorHandler("httpJobHandler");
        xxlJobInfo.setExecutorParam("url:http://" + sceneIp + ":21000/scene/timer/execWeather\n" +
                "method:GET\n" +
                "data:");
        xxlJobInfo.setExecutorBlockStrategy("SERIAL_EXECUTION");
        xxlJobInfo.setExecutorTimeout(0);
        xxlJobInfo.setExecutorFailRetryCount(0);
        xxlJobInfo.setGlueType("BEAN");
        xxlJobInfo.setGlueRemark("GLUE代码初始化");
        xxlJobInfoService.add(xxlJobInfo);
        return true;
    }

}